﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.Functions;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.基础数据
{
    /// <summary>
    /// 货架
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShelfController : ControllerBase
    {
        private readonly ILogRepository logRepository;
        public ShelfController(IShelvesRepository shelfService, ILogRepository logRepository)
        {
            ShelfService = shelfService;
            this.logRepository = logRepository;
        }
        public IShelvesRepository ShelfService { get; }
        /// <summary>
        /// 添加货架
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddShel(ShelvesDto obj)
        {
            if (string.IsNullOrEmpty(obj.Code))
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            string message = "";
            bool IsOK = ShelfService.AddShel(obj,out message);
            if (IsOK)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了货架" + obj.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！"+message, "500"));
        }
        /// <summary>
        /// 显示货架信息
        /// </summary>
        /// <param name="hcode"></param>
        /// <param name="hname"></param>
        /// <param name="kid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShelList(string? hname = "", int kid = 0, int pageIndex = 1, int pageSize = 3)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            ApiResult apiresult = new ApiResult();
            int totalCount;
            var list = ShelfService.GetShelList(hname, kid, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                apiresult.code = "200";
                apiresult.message = "获取成功!";
                apiresult.data = list;
                apiresult.count = totalCount;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }
        /// <summary>
        /// 删除货架信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelShel(int uid)
        {
            if (uid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = ShelfService.DelShel(uid);
            if (IsOK)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了货架";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
        /// <summary>
        /// 修改货架信息
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateShel(ShelvesDto u)
        {
            if (u.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if(string.IsNullOrEmpty(u.Name))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            string message = "";
            var IsOK = ShelfService.UpdateShel(u,out message);
            if (IsOK)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 6;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了货架" + u.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！"+message, "500"));
        }
        /// <summary>
        /// 根据id获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShelById(int id)
        {
            var res = ShelfService.GetShelById(id);
            ApiResult apiresult = new ApiResult();
            if (res != null)
            {
                apiresult.code = "200";
                apiresult.message = "获取成功!";
                apiresult.data = res;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }
        /// <summary>
        /// 获取库房(仓库)信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTwarehouseList()
        {
            var res = ShelfService.GetTwarehouseList();
            ApiResult apiresult = new ApiResult();
            if (res != null)
            {
                apiresult.code = "200";
                apiresult.message = "获取成功!";
                apiresult.data = res;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }      

        /// <summary>
        /// 所属库区显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetWarehouseMenuitem()
        {
            var list = ShelfService.GetWarehouseMenuitem();
            if(list!=null)
            {
                return Ok(BaseResult.Result(list, "获取成功！"));
            }
            return Ok(BaseResult.Result(null, "获取失败！"));
        }

        /// <summary>
        /// 枚举货架类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEmShelfType()
        {
            var list = BaseEnum.GetCodeRulesList<EmShelfType>();

            return Ok(list);
        }

        /// <summary>
        /// 根据库区获取货架
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAreaShelf(int aid=0)
        {
            if(aid<=0)
            {
                return Ok(BaseResult.Result("400", "库区Id有误！"));
            }
            var list= ShelfService.GetAreaShelf(aid);
            if(list!=null)
            {
                return Ok(BaseResult.Result(list, "获取成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500", 0));
        }
    }
}
